<?php

	include("common.php");
	
	if(!empty($_POST)){
		mysql_query("UPDATE `197_teachers` SET `password` = '" . md5($_POST['password']) . "', `name` = '" . $_POST['name'] . "' WHERE `teacher_id` = " . $_POST['teacher_id']) or die(mysql_error());
		include("header.php");
		
?>
<?php if(!AJAX){ ?><h1>修改教師</h1><?php } ?>
<p>修改成功<?php if(!AJAX){ ?>，請<a href="teacherFind.php">按此</a>返回<?php } ?>。</p>
<?php

		include("sidebar.php");
		include("footer.php");
		exit;
	
	}
	
	$result = mysql_query("SELECT * FROM `197_teachers` WHERE `teacher_id` = " . $_GET['teacher_id']);
	if(!mysql_num_rows($result)){
		include("header.php");
		
?>
<h1>修改教師</h1>
<p>查無此教師，請<a href="teacherFind.php">按此</a>返回。</p>
<?php

		include("sidebar.php");
		include("footer.php");
		
		exit;
		
	}

	$teacher = mysql_fetch_assoc($result);
	
?>
<?php include("header.php"); ?>
<h1>修改教師</h1>
<div id="edit-result-dialog">
</div>
<script type="text/javascript" defer="defer">
	$("#edit-result-dialog").dialog({
		width: 300,
		height: 210,
		autoOpen: false,
		title: "修改教師",
		modal: true,
		draggable: false,
		resizable: false
	});
	function editTeacher(){
		$("#edit-result-dialog").dialog("option", "buttons", { "關閉": function(){ location.replace("teacherFind.php"); } });
		$("#edit-result-dialog").html("<img src='../images/ajax-loader.gif' />");
		$("#edit-result-dialog").dialog("open");
		$.post("teacherEdit.php?ajax=true", { password: $("#password").val(), name: $("#name").val(), teacher_id: $("#teacher_id").val() }, function(r){ $("#edit-result-dialog").html(r) });
	}
</script>
<form action="teacherEdit.php" onsubmit="editTeacher(); return false;" method="POST">
	<p>帳號：<?php echo $teacher['username']; ?><input type="hidden" name="teacher_id" value="<?php echo $teacher['teacher_id']; ?>" id="teacher_id" /></p>
	<p><label>密碼：<input type="password" name="password" id="password" /></label></p>
	<p><label>姓名：<input type="text" name="name" value="<?php echo $teacher['name']; ?>" id="name" /></label></p>
	<p><input type="submit" value="修改" />｜<a href="teacherFind.php">返回</a></p>
</form>
<?php include("sidebar.php"); ?>
<?php include("footer.php"); ?>